package so.shanku.essential.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;
import net.tsz.afinal.json.JsonMap;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import so.shanku.essential.R;
import so.shanku.essential.utils.Constant;
import so.shanku.essential.view.ListViewNoScroll;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/9/9.
 * 退货进度列表 适配器
 */
public class ReturnOrderAdapter extends MyBaseAdapter {


	/**
	 * 操作订单的点击监听
	 */
	private ReturnOrderItemCompountClickListener returnOrderItemCompountClickListener;

	public void setReturnOrderItemCompountClickListener(ReturnOrderItemCompountClickListener returnOrderItemCompountClickListener) {
		this.returnOrderItemCompountClickListener = returnOrderItemCompountClickListener;
	}

	public interface ReturnOrderItemCompountClickListener {
		void returnOrderItemCompountClick(int position, Constant.UserOrderItemType returnOrderItemType,int childPosition);
	}

	public ReturnOrderAdapter(Context context) {
		super(context);
	}

	public ReturnOrderAdapter(Context context, List<? extends Map<String, ?>> datas) {
		super(context, datas);
	}

//
//	{"GoodsReturnNum":"H000056","ProductList":[],
//		"Status":"退货完成","CreateTimeStr":"2015-12-08",
//			"ReturnGoodsButton":false,"ReturnGoodsDetailsButton":true}
	@Override
	public View getView(int i, View view, ViewGroup viewGroup) {
		ViewHolder viewHolder = null;
		if (view == null) {
			view = layoutInflater.inflate(R.layout.item_return_order, viewGroup, false);
			viewHolder = new ViewHolder(view);
			view.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) view.getTag();
		}
		final int itemPosition=i;
		final JsonMap jsonMap = (JsonMap) datas.get(i);
		viewHolder.tv_return_goods_order.setText(jsonMap.getStringNoNull("GoodsReturnNum"));
		viewHolder.tv_apply_time.setText(jsonMap.getStringNoNull("CreateTimeStr"));
		viewHolder.return_goods_statue_tv.setText(jsonMap.getStringNoNull("Status"));
		final List<JsonMap<String, Object>> dataHLV = jsonMap.getList_JsonMap("ProductList");
		viewHolder.listview_item_goods.setAdapter(new ReturnProductOrderAdapter(context, dataHLV));
		viewHolder.listview_item_goods.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


				if (returnOrderItemCompountClickListener != null) {
					returnOrderItemCompountClickListener.returnOrderItemCompountClick(itemPosition, Constant.UserOrderItemType.SEE_PRODUCT,position);
				}
			}
		});

		//JsonMap optionButtonMap = jsonMap.getJsonMap("ShowOrderButton");


		viewHolder.return_goods_detail_tv.setVisibility(jsonMap.getBoolean("ReturnGoodsDetailsButton", false) ? View.VISIBLE : View.GONE);
		viewHolder.recruitment_tv.setVisibility(jsonMap.getBoolean("IsSeeLogistics ", false) ? View.VISIBLE : View.GONE);
		viewHolder.tv_do_goods_return.setVisibility(jsonMap.getBoolean("ReturnGoodsButton", false) ? View.VISIBLE : View.GONE);
		viewHolder.refund_detail_tv.setVisibility(jsonMap.getBoolean("IsArefundDetails", false) ? View.VISIBLE : View.GONE);
		setViewTag(viewHolder, i);
		setClickListener(viewHolder);
		return view;
	}

	/**
	 * s设置点击事件
	 *
	 * @param viewHolder
	 */
	private void setClickListener(ViewHolder viewHolder) {
		viewHolder.tv_scan_progress.setOnClickListener(onClickListener);
		viewHolder.return_goods_detail_tv.setOnClickListener(onClickListener);
		viewHolder.recruitment_tv.setOnClickListener(onClickListener);
		viewHolder.tv_do_goods_return.setOnClickListener(onClickListener);
		viewHolder.refund_detail_tv.setOnClickListener(onClickListener);

		// viewHolder.listview_item_order.setOnClickListener(onClickListener);
	}

	private View.OnClickListener onClickListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			int viewId = v.getId();
			Integer tag = (Integer) v.getTag();
			switch (viewId) {
				case R.id.tv_scan_progress:
					if (returnOrderItemCompountClickListener != null) {
						returnOrderItemCompountClickListener.returnOrderItemCompountClick(tag, Constant.UserOrderItemType.GOODS_RETURN_PROGRESS,-1);
					}
					break;
				case R.id.return_goods_detail_tv:
					if (returnOrderItemCompountClickListener != null) {
						returnOrderItemCompountClickListener.returnOrderItemCompountClick(tag, Constant.UserOrderItemType.GOODS_RETURN_DETAIL,-1);
					}
					break;

				case R.id.recruitment_tv:
					if (returnOrderItemCompountClickListener != null) {
						returnOrderItemCompountClickListener.returnOrderItemCompountClick(tag, Constant.UserOrderItemType.SEELogistics,-1);
					}
					break;

				case R.id.tv_do_goods_return:
					if (returnOrderItemCompountClickListener != null) {
						returnOrderItemCompountClickListener.returnOrderItemCompountClick(tag, Constant.UserOrderItemType.DO_GOODS_RETURN,-1);
					}
					break;
				case R.id.refund_detail_tv:
					if (returnOrderItemCompountClickListener != null) {
						returnOrderItemCompountClickListener.returnOrderItemCompountClick(tag, Constant.UserOrderItemType.REFUND_DETAIL,-1);
					}
					break;

				default:
			}


		}
	};

	/**
	 * 设置标记
	 *
	 * @param viewHolder
	 * @param position
	 */
	private void setViewTag(ViewHolder viewHolder, int position) {
		viewHolder.tv_scan_progress.setTag(position);
		viewHolder.return_goods_detail_tv.setTag(position);
		viewHolder.recruitment_tv.setTag(position);
		viewHolder.tv_do_goods_return.setTag(position);

		viewHolder.refund_detail_tv.setTag(position);
	}

	private class ViewHolder {

		/**
		 * 退货单号
		 */
		@ViewInject(R.id.tv_return_goods_order)
		private TextView tv_return_goods_order;

		/**
		 * 进度查询
		 */
		@ViewInject(R.id.tv_scan_progress)
		private TextView tv_scan_progress;

		/**
		 * 退货状态
		 */
		@ViewInject(R.id.return_goods_statue_tv)
		private TextView return_goods_statue_tv;

		/**
		 * 申请时间
		 */
		@ViewInject(R.id.tv_apply_time)
		private TextView tv_apply_time;


		/**
		 * 商品信息
		 */
		@ViewInject(R.id.listview_item_goods)
		private ListViewNoScroll listview_item_goods;
		//以下是按钮操作
		/**
		 * 退货详情
		 */
		@ViewInject(R.id.return_goods_detail_tv)
		private TextView return_goods_detail_tv;

		/**
		 * 进行退货
		 */
		@ViewInject(R.id.tv_do_goods_return)
		private TextView tv_do_goods_return;

		/**
		 * 退款详情
		 */
		@ViewInject(R.id.refund_detail_tv)
		private TextView refund_detail_tv;

		/**
		 * 退货物流详情
		 */
		@ViewInject(R.id.recruitment_tv)
		private TextView recruitment_tv;


		public ViewHolder(View view) {
			ViewUtils.inject(this, view);
		}
	}
}
